package data.mysql;

import java.util.List;

import bean.User;
import data.IUserDao;
import data.exception.DaoException;

/**
 * User DAO
 * 
 * @author chuxiaoyuan
 */
public class UserDao extends BaseDaoSupport<User> implements IUserDao {

	/* (non-Javadoc)
	 * @see data.mysql.BaseDaoSupport#createBean()
	 */
	@Override
	protected User createBean() {

		return new User();
	}

	/* (non-Javadoc)
	 * @see data.IUserDao#selectByAccountAndPassword(
	 * 		java.lang.String, java.lang.String)
	 */
	@Override
	public List<User> selectByAccountAndPassword(
			String account, String password) throws DaoException {

		// Build SQL
		String preparedSQL = this.buildSelectClause(
				this.getFields(), this.getTableName());
		preparedSQL += " WHERE account=? AND password=?";
		
		// Execute query
		List<User> results = this.executeQuery(
				this, this.getFields(), 
				preparedSQL, account, password);
		
		// Return bean
		return results;
	}

	/* (non-Javadoc)
	 * @see data.IUserDao#updatePassword(bean.User)
	 */
	@Override
	public int updatePassword(User user) throws DaoException {

		// Build SQL
		String preparedSQL = "UPDATE " + this.getTableName()
				+ " SET password=? WHERE account=?";

		// Execute update
		return this.executeUpdate(
				preparedSQL, user.getPassword(), user.getAccount());
	}

}
